<template>
  <div class="exam-booking-page">
    <div class="page-header">
      <h2>考试报名</h2>
      <p>选择您要参加的考试并完成报名</p>
    </div>

    <!-- 考试类型筛选 -->
    <div class="filter-section">
      <el-tabs v-model="activeTab" @tab-click="handleTabClick">
        <el-tab-pane label="全部考试" name="all">
          <div class="exam-grid">
            <div v-for="exam in filteredExams" :key="exam.id" class="exam-card">
              <div class="exam-header">
                <!--<div class="exam-icon">-->
                  <!--<img :src="exam.icon" :alt="exam.name" />-->
                <!--</div>-->
                <div class="exam-info">
                  <h3>{{ exam.name }}</h3>
                  <p>{{ exam.description }}</p>
                  <div class="exam-tags">
                    <el-tag :type="exam.level === '初级' ? 'success' : exam.level === '中级' ? 'warning' : 'danger'">
                      {{ exam.level }}
                    </el-tag>
                    <el-tag type="info">{{ exam.duration }}</el-tag>
                  </div>
                </div>
              </div>
              
              <div class="exam-details">
                <div class="detail-item">
                  <span class="label">考试费用：</span>
                  <span class="value price">¥{{ exam.price }}</span>
                </div>
                <div class="detail-item">
                  <span class="label">考试时间：</span>
                  <span class="value">{{ exam.examDate }}</span>
                </div>
                <div class="detail-item">
                  <span class="label">报名截止：</span>
                  <span class="value">{{ exam.deadline }}</span>
                </div>
                <div class="detail-item">
                  <span class="label">考试地点：</span>
                  <span class="value">{{ exam.location }}</span>
                </div>
              </div>
              
              <div class="exam-actions">
                <el-button 
                  type="primary" 
                  @click="bookExam(exam)"
                  :disabled="exam.status === '已报名' || exam.status === '已截止'"
                >
                  {{ getActionText(exam.status) }}
                </el-button>
                <!--<el-button @click="viewExamDetail(exam)">查看详情</el-button>-->
              </div>
              
              <div class="exam-status">
                <el-tag :type="getStatusType(exam.status)">
                  {{ exam.status }}
                </el-tag>
              </div>
            </div>
          </div>
        </el-tab-pane>
        
        <el-tab-pane label="JFT BASIC" name="jft">
          <div class="exam-grid">
            <div v-for="exam in jftExams" :key="exam.id" class="exam-card">
              <!-- 同上的考试卡片结构 -->
              <div class="exam-header">
                <!--<div class="exam-icon">-->
                  <!--<img :src="exam.icon" :alt="exam.name" />-->
                <!--</div>-->
                <div class="exam-info">
                  <h3>{{ exam.name }}</h3>
                  <p>{{ exam.description }}</p>
                  <div class="exam-tags">
                    <el-tag type="success">{{ exam.level }}</el-tag>
                    <el-tag type="info">{{ exam.duration }}</el-tag>
                  </div>
                </div>
              </div>
              
              <div class="exam-details">
                <div class="detail-item">
                  <span class="label">考试费用：</span>
                  <span class="value price">¥{{ exam.price }}</span>
                </div>
                <div class="detail-item">
                  <span class="label">考试时间：</span>
                  <span class="value">{{ exam.examDate }}</span>
                </div>
                <div class="detail-item">
                  <span class="label">报名截止：</span>
                  <span class="value">{{ exam.deadline }}</span>
                </div>
                <div class="detail-item">
                  <span class="label">考试地点：</span>
                  <span class="value">{{ exam.location }}</span>
                </div>
              </div>
              
              <div class="exam-actions">
                <el-button 
                  type="primary" 
                  @click="bookExam(exam)"
                  :disabled="exam.status === '已报名' || exam.status === '已截止'"
                >
                  {{ getActionText(exam.status) }}
                </el-button>
                <!--<el-button @click="viewExamDetail(exam)">查看详情</el-button>-->
              </div>
              
              <div class="exam-status">
                <el-tag :type="getStatusType(exam.status)">
                  {{ exam.status }}
                </el-tag>
              </div>
            </div>
          </div>
        </el-tab-pane>
        
        <el-tab-pane label="特定技能" name="tokutei">
          <div class="exam-grid">
            <div v-for="exam in tokuteiExams" :key="exam.id" class="exam-card">
              <!-- 同上的考试卡片结构 -->
              <div class="exam-header">
                <!--<div class="exam-icon">-->
                  <!--<img :src="exam.icon" :alt="exam.name" />-->
                <!--</div>-->
                <div class="exam-info">
                  <h3>{{ exam.name }}</h3>
                  <p>{{ exam.description }}</p>
                  <div class="exam-tags">
                    <el-tag type="warning">{{ exam.level }}</el-tag>
                    <el-tag type="info">{{ exam.duration }}</el-tag>
                  </div>
                </div>
              </div>
              
              <div class="exam-details">
                <div class="detail-item">
                  <span class="label">考试费用：</span>
                  <span class="value price">¥{{ exam.price }}</span>
                </div>
                <div class="detail-item">
                  <span class="label">考试时间：</span>
                  <span class="value">{{ exam.examDate }}</span>
                </div>
                <div class="detail-item">
                  <span class="label">报名截止：</span>
                  <span class="value">{{ exam.deadline }}</span>
                </div>
                <div class="detail-item">
                  <span class="label">考试地点：</span>
                  <span class="value">{{ exam.location }}</span>
                </div>
              </div>
              
              <div class="exam-actions">
                <el-button 
                  type="primary" 
                  @click="bookExam(exam)"
                  :disabled="exam.status === '已报名' || exam.status === '已截止'"
                >
                  {{ getActionText(exam.status) }}
                </el-button>
                <!--<el-button @click="viewExamDetail(exam)">查看详情</el-button>-->
              </div>
              
              <div class="exam-status">
                <el-tag :type="getStatusType(exam.status)">
                  {{ exam.status }}
                </el-tag>
              </div>
            </div>
          </div>
        </el-tab-pane>
        
        <el-tab-pane label="JLPT" name="jlpt">
          <div class="exam-grid">
            <div v-for="exam in jlptExams" :key="exam.id" class="exam-card">
              <!-- 同上的考试卡片结构 -->
              <div class="exam-header">
                <!--<div class="exam-icon">-->
                  <!--<img :src="exam.icon" :alt="exam.name" />-->
                <!--</div>-->
                <div class="exam-info">
                  <h3>{{ exam.name }}</h3>
                  <p>{{ exam.description }}</p>
                  <div class="exam-tags">
                    <el-tag type="danger">{{ exam.level }}</el-tag>
                    <el-tag type="info">{{ exam.duration }}</el-tag>
                  </div>
                </div>
              </div>
              
              <div class="exam-details">
                <div class="detail-item">
                  <span class="label">考试费用：</span>
                  <span class="value price">¥{{ exam.price }}</span>
                </div>
                <div class="detail-item">
                  <span class="label">考试时间：</span>
                  <span class="value">{{ exam.examDate }}</span>
                </div>
                <div class="detail-item">
                  <span class="label">报名截止：</span>
                  <span class="value">{{ exam.deadline }}</span>
                </div>
                <div class="detail-item">
                  <span class="label">考试地点：</span>
                  <span class="value">{{ exam.location }}</span>
                </div>
              </div>
              
              <div class="exam-actions">
                <el-button 
                  type="primary" 
                  @click="bookExam(exam)"
                  :disabled="exam.status === '已报名' || exam.status === '已截止'"
                >
                  {{ getActionText(exam.status) }}
                </el-button>
                <!--<el-button @click="viewExamDetail(exam)">查看详情</el-button>-->
              </div>
              
              <div class="exam-status">
                <el-tag :type="getStatusType(exam.status)">
                  {{ exam.status }}
                </el-tag>
              </div>
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>

    <!-- 报名确认对话框 -->
    <el-dialog
      v-model="bookingDialogVisible"
      title="确认报名"
      width="500px"
      :before-close="handleCloseBookingDialog"
    >
      <div v-if="selectedExam" class="booking-confirm">
        <div class="confirm-item">
          <span class="label">考试名称：</span>
          <span class="value">{{ selectedExam.name }}</span>
        </div>
        <div class="confirm-item">
          <span class="label">考试时间：</span>
          <span class="value">{{ selectedExam.examDate }}</span>
        </div>
        <div class="confirm-item">
          <span class="label">考试地点：</span>
          <span class="value">{{ selectedExam.location }}</span>
        </div>
        <div class="confirm-item">
          <span class="label">考试费用：</span>
          <span class="value price">¥{{ selectedExam.price }}</span>
        </div>
        
        <el-divider />
        
        <div class="payment-method">
          <h4>选择支付方式</h4>
          <el-radio-group v-model="paymentMethod">
            <el-radio label="alipay">支付宝</el-radio>
            <el-radio label="wechat">微信支付</el-radio>
            <el-radio label="bank">银行转账</el-radio>
          </el-radio-group>
        </div>
      </div>
      
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="bookingDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmBooking" :loading="bookingLoading">
            确认报名并支付
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()

// 当前激活的标签页
const activeTab = ref('all')

// 对话框状态
const bookingDialogVisible = ref(false)
const bookingLoading = ref(false)
const selectedExam = ref(null)
const paymentMethod = ref('alipay')

// 考试数据
const allExams = ref([
  {
    id: 1,
    name: 'JFT BASIC 基础考试',
    description: '日本技术实习培训日语基础考试',
    level: '初级',
    duration: '60分钟',
    price: 300,
    examDate: '2024-02-15',
    deadline: '2024-02-10',
    location: '北京市海淀区',
    status: '可报名',
    type: 'jft',
    icon: 'https://via.placeholder.com/60x60/409EFF/ffffff?text=JFT'
  },
  {
    id: 2,
    name: '特定技能 食品加工',
    description: '食品加工特定技能考试',
    level: '中级',
    duration: '90分钟',
    price: 500,
    examDate: '2024-02-20',
    deadline: '2024-02-15',
    location: '上海市徐汇区',
    status: '可报名',
    type: 'tokutei',
    icon: 'https://via.placeholder.com/60x60/67C23A/ffffff?text=特定技能'
  },
  {
    id: 3,
    name: 'JLPT N3',
    description: '日本语能力测试N3级',
    level: '高级',
    duration: '120分钟',
    price: 400,
    examDate: '2024-03-10',
    deadline: '2024-03-05',
    location: '广州市天河区',
    status: '可报名',
    type: 'jlpt',
    icon: 'https://via.placeholder.com/60x60/E6A23C/ffffff?text=JLPT'
  },
  {
    id: 4,
    name: 'JFT BASIC 模拟考试',
    description: 'JFT BASIC 模拟考试',
    level: '初级',
    duration: '60分钟',
    price: 100,
    examDate: '2024-01-25',
    deadline: '2024-01-20',
    location: '在线考试',
    status: '已报名',
    type: 'jft',
    icon: 'https://via.placeholder.com/60x60/409EFF/ffffff?text=JFT'
  },
  {
    id: 5,
    name: '特定技能 护理服务',
    description: '护理服务特定技能考试',
    level: '中级',
    duration: '90分钟',
    price: 600,
    examDate: '2024-01-30',
    deadline: '2024-01-25',
    location: '深圳市南山区',
    status: '已截止',
    type: 'tokutei',
    icon: 'https://via.placeholder.com/60x60/67C23A/ffffff?text=特定技能'
  }
])

// 按类型筛选考试
const jftExams = computed(() => allExams.value.filter(exam => exam.type === 'jft'))
const tokuteiExams = computed(() => allExams.value.filter(exam => exam.type === 'tokutei'))
const jlptExams = computed(() => allExams.value.filter(exam => exam.type === 'jlpt'))
const filteredExams = computed(() => allExams.value)

// 处理标签页点击
const handleTabClick = (tab) => {
  console.log('切换到标签页:', tab.props.name)
}

// 获取操作按钮文本
const getActionText = (status) => {
  switch (status) {
    case '可报名': return '立即报名'
    case '已报名': return '已报名'
    case '已截止': return '报名截止'
    default: return '立即报名'
  }
}

// 获取状态类型
const getStatusType = (status) => {
  switch (status) {
    case '可报名': return 'success'
    case '已报名': return 'warning'
    case '已截止': return 'danger'
    default: return 'info'
  }
}

// 报名考试
const bookExam = (exam) => {
//  selectedExam.value = exam
  ElMessage.success('报名成功！')
//  bookingDialogVisible.value = true
}

// 查看考试详情
const viewExamDetail = (exam) => {
  ElMessage.info(`查看 ${exam.name} 的详细信息`)
  // 这里可以跳转到考试详情页面
}

// 确认报名
const confirmBooking = async () => {
  if (!selectedExam.value) return
  
  bookingLoading.value = true
  
  try {
    // 模拟API调用
    await new Promise(resolve => setTimeout(resolve, 2000))
    
    ElMessage.success('报名成功！请及时完成支付')
    bookingDialogVisible.value = false
    
    // 更新考试状态
    selectedExam.value.status = '已报名'
    
  } catch (error) {
    ElMessage.error('报名失败，请重试')
  } finally {
    bookingLoading.value = false
  }
}

// 关闭报名对话框
const handleCloseBookingDialog = () => {
  bookingDialogVisible.value = false
  selectedExam.value = null
  paymentMethod.value = 'alipay'
}
</script>

<style scoped>
.exam-booking-page {
  min-height: 100vh;
  background: #f5f7fa;
  padding: 20px 0;
}

.page-header {
  text-align: center;
  margin-bottom: 30px;
}

.page-header h2 {
  color: #2c3e50;
  margin: 0 0 10px 0;
  font-size: 2rem;
}

.page-header p {
  color: #666;
  margin: 0;
  font-size: 1.1rem;
}

.filter-section {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.exam-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 25px;
  margin-top: 20px;
}

.exam-card {
  background: white;
  border-radius: 12px;
  padding: 25px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  position: relative;
}

.exam-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.exam-header {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
}

.exam-icon {
  width: 60px;
  height: 60px;
  border-radius: 8px;
  overflow: hidden;
  flex-shrink: 0;
}

.exam-icon img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.exam-info h3 {
  margin: 0 0 8px 0;
  color: #2c3e50;
  font-size: 1.2rem;
}

.exam-info p {
  margin: 0 0 12px 0;
  color: #666;
  font-size: 0.9rem;
  line-height: 1.4;
}

.exam-tags {
  display: flex;
  gap: 8px;
}

.exam-details {
  margin-bottom: 20px;
}

.detail-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
  font-size: 0.9rem;
}

.detail-item .label {
  color: #666;
}

.detail-item .value {
  color: #2c3e50;
  font-weight: 500;
}

.detail-item .price {
  color: #f56c6c;
  font-weight: bold;
  font-size: 1.1rem;
}

.exam-actions {
  display: flex;
  gap: 10px;
  margin-bottom: 15px;
}

.exam-status {
  position: absolute;
  top: 15px;
  right: 15px;
}

.booking-confirm {
  padding: 20px 0;
}

.confirm-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 12px;
  font-size: 0.95rem;
}

.confirm-item .label {
  color: #666;
  font-weight: 500;
}

.confirm-item .value {
  color: #2c3e50;
  font-weight: bold;
}

.confirm-item .price {
  color: #f56c6c;
  font-size: 1.1rem;
}

.payment-method {
  margin-top: 20px;
}

.payment-method h4 {
  margin: 0 0 15px 0;
  color: #2c3e50;
}

.el-radio-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .exam-grid {
    grid-template-columns: 1fr;
  }
  
  .exam-header {
    flex-direction: column;
    text-align: center;
  }
  
  .exam-actions {
    flex-direction: column;
  }
  
  .exam-status {
    position: static;
    text-align: center;
    margin-top: 15px;
  }
}
</style> 